package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectAttributeActivity;
import com.hongxun.design.activitys.StatisticsSearchActivity;
import com.hongxun.design.bean.SearchBean;
import com.hongxun.design.bean.UnitBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.pickerview.OptionsPopupWindow;
import com.hongxun.design.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/13.
 */
public class ProjectStatisticSearchAdapter extends BaseAdapter {
    Context context;
    List<SearchBean> itemData;
    LayoutInflater inflater;
    Handler handler;

    public ProjectStatisticSearchAdapter(Context context, List<SearchBean> itemData, Handler handler) {
        this.context = context;
        this.itemData = itemData;
        this.handler = handler;
        inflater = LayoutInflater.from(context);
    }

    public void setValue(int p, Object data) {
        UnitBean bean = (UnitBean) data;
        itemData.get(p).setValue(bean.getDepartname());
        itemData.get(p).setValueCode(data);
        notifyDataSetChanged();
    }

    public View getLayout(int layout) {
        return inflater.inflate(layout, null);
    }

    @Override
    public int getCount() {
        return itemData.size();
    }

    @Override
    public Object getItem(int i) {
        return itemData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        return itemData.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        int type = getItemViewType(i);
        //TimeHoldler timeHoldler = null;
        //ButtonHoldler buttonHoldler = null;
        ChoiceHoldler choiceHoldler = null;
        FinishHoldler finishHoldler = null;
        if (view == null) {
            if (type==Constant.Search_finish){
                view = getLayout(R.layout.listview_item_search_finish);
                finishHoldler = new FinishHoldler(view);
                view.setTag(finishHoldler);
            }else{
                view = getLayout(R.layout.listview_item_statis_item);
                choiceHoldler = new ChoiceHoldler(view);
                view.setTag(choiceHoldler);
            }
//            switch (type) {
//                case Constant.Search_Time:
//                    view = getLayout(R.layout.listview_item_search_time);
//                    timeHoldler = new TimeHoldler(view);
//                    view.setTag(timeHoldler);
//                    break;
//                case Constant.Search_ChoiceButton:
//                    view = getLayout(R.layout.listview_item_search_button);
//                    buttonHoldler = new ButtonHoldler(view);
//                    view.setTag(buttonHoldler);
//                    break;
//                case Constant.Search_Choice:
//                    view = getLayout(R.layout.listview_item_search_choice);
//                    choiceHoldler = new ChoiceHoldler(view);
//                    view.setTag(choiceHoldler);
//                    break;
//
//                case Constant.Search_finish:
//
//                    break;
//            }
        } else {

            if (type==Constant.Search_finish){
                finishHoldler = (FinishHoldler) view.getTag();
            }else{
                choiceHoldler = (ChoiceHoldler) view.getTag();
            }
//            switch (type) {
//                case Constant.Search_Time:
//                    timeHoldler = (TimeHoldler) view.getTag();
//                    break;
//                case Constant.Search_ChoiceButton:
//                    buttonHoldler = (ButtonHoldler) view.getTag();
//                    break;
//                case Constant.Search_Choice:
//                    choiceHoldler = (ChoiceHoldler) view.getTag();
//                    break;
//                case Constant.Search_finish:
//                    finishHoldler = (FinishHoldler) view.getTag();
//                    break;
//            }
        }
        SearchBean bean = null;
        try {
//            switch (type) {
//                case Constant.Search_Time:
//                    bean = (SearchBean) getItem(i);
//                    timeHoldler.title.setText(bean.getTitle());
//                    SetTypeface.set(timeHoldler.choice, bean.getIcon());
//                    timeHoldler.timevalue.setText(bean.getValue());
//                    timeHoldler.timebutton.setOnClickListener(new MClickListener(i, bean) {
//                        @Override
//                        public void onClick(View view, int position, Object msg) {
//                            final SearchBean bean = (SearchBean) msg;
//                            //打开时间选择器
//                            final OptionsPopupWindow pwOptions = new OptionsPopupWindow(context);
//                            //选项1
//                            //三级联动效果
//                            pwOptions.setPickerTime();
//                            pwOptions.setItemLength(5);
//                            final ArrayList<String> startTime = pwOptions.getpicker1();
//                            final ArrayList<ArrayList<String>> endTime = pwOptions.getpicker2();
//                            //监听确定选择按钮
//                            pwOptions.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
//                                @Override
//                                public void onOptionsSelect(int options1, int option2, int options3) {
//                                    //返回的分别是三个级别的选中位置
//                                    String first = startTime.get(options1);
//                                    String end = endTime.get(options1).get(option2);
//                                    bean.setValue(first + "—" + end);
//                                    bean.setValueCode(first + "—" + end);
//                                    notifyDataSetChanged();
//                                }
//                            });
//                            pwOptions.showAtLocation(view, Gravity.BOTTOM, 0, 0);
//
//                        }
//                    });
//                    break;
//                case Constant.Search_ChoiceButton:
//                    bean = (SearchBean) getItem(i);
//                    buttonHoldler.title.setText(bean.getTitle());
//                    addViewbutton(buttonHoldler.search_l_button, bean);
//                    break;
//                case Constant.Search_Choice:
//                    bean = (SearchBean) getItem(i);
//                    if (bean.isDisplayTitle()) {
//                        choiceHoldler.title.setVisibility(View.VISIBLE);
//                        choiceHoldler.title.setText(bean.getTitle());
//                    } else {
//                        choiceHoldler.title.setVisibility(View.GONE);
//                    }
//                    choiceHoldler.name.setText(bean.getName());
//                    choiceHoldler.value.setText(bean.getValue());
//                    SetTypeface.set(choiceHoldler.button, bean.getIcon());
//                    choiceHoldler.choicebutton.setOnClickListener(new MClickListener(i, bean) {
//                        @Override
//                        public void onClick(View view, int position, Object msg) {
//                            //选择界面
//                            SearchBean bean = (SearchBean) msg;
//                            Bundle bundle = new Bundle();
//                            bundle.putString(MString.TITLE, bean.getName());
//                            if (Constant.设计单位.equals(bean.getName())) {
//                                bundle.putString(MString.SENDCODE, "12");
//                            } else {
//                                bundle.putString(MString.SENDCODE, "11");
//                            }
//                            bundle.putString(MString.TYPE, Constant.ACTIVITYRADIO);
//                            bundle.putString(MString.TYPENUM, position + "");
//                            bundle.putString(MString.CODE, "1");
//                            IntentToActivity.intent((Activity) context, CreateProjectAttributeActivity.class, bundle);
//                        }
//                    });
//                    break;
//                case Constant.Search_finish:

            if (type==Constant.Search_finish){

                SetTypeface.set(finishHoldler.finish, IconSourse.SEARCH);
                SetTypeface.set(finishHoldler.reflash, IconSourse.Reset);
                finishHoldler.finish.append(" 搜索");
                finishHoldler.reflash.append(" 重置");
                finishHoldler.finish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        handler.sendEmptyMessage(((StatisticsSearchActivity) context).SEARCH);
                    }
                });

                finishHoldler.reflash.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        handler.sendEmptyMessage(((StatisticsSearchActivity) context).UPDATE);
                    }
                });
            }else{
                bean = (SearchBean) getItem(i);
                choiceHoldler.name.setText(bean.getName());
                choiceHoldler.value.setText(bean.getValue());
                if (bean.getNameCode().equals(MString.year)){
                    SetTypeface.set(choiceHoldler.right,IconSourse.Calendar);
                    choiceHoldler.item_button.setOnClickListener(new MClickListener(i,bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            final SearchBean bean = (SearchBean) msg;
//                            //打开时间选择器
                            final OptionsPopupWindow pwOptions = new OptionsPopupWindow(context);
                            //选项1
                            //三级联动效果
                            pwOptions.setPickerTime();
                            pwOptions.setItemLength(10);
                            final ArrayList<String> startTime = pwOptions.getpicker1();
                            final ArrayList<ArrayList<String>> endTime = pwOptions.getpicker2();
                            //监听确定选择按钮
                            pwOptions.setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int option2, int options3) {
                                    //返回的分别是三个级别的选中位置
                                    String first = startTime.get(options1);
                                    String end = endTime.get(options1).get(option2);
                                    bean.setValue(first + "—" + end);
                                    bean.setValueCode(first + "—" + end);
                                    notifyDataSetChanged();
                                }
                            });
                            pwOptions.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                        }
                    });
                }else{
                    SetTypeface.set(choiceHoldler.right,IconSourse.RIGHTARROW);
                    choiceHoldler.item_button.setOnClickListener(new MClickListener(i,bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            SearchBean bean = (SearchBean) msg;
                            Bundle bundle = new Bundle();
                            bundle.putString(MString.TITLE, bean.getName());
                            if (Constant.设计单位.equals(bean.getName())) {
                                bundle.putString(MString.SENDCODE, "12");
                            } else {
                                bundle.putString(MString.SENDCODE, "11");
                            }
                            bundle.putString(MString.TYPE, Constant.ACTIVITYRADIO);
                            bundle.putString(MString.TYPENUM, position + "");
                            bundle.putString(MString.CODE, "StatisticsSearchActivity");
                            IntentToActivity.intent((Activity) context, CreateProjectAttributeActivity.class, bundle);
                        }
                    });
                }


            }

//                    break;
//            }
        } catch (Exception e) {
        }

        return view;
    }

    class TimeHoldler {
        TextView title, timevalue, choice;
        RelativeLayout timebutton;

        TimeHoldler(View v) {
            title = (TextView) v.findViewById(R.id.search_itemtitle);
            timevalue = (TextView) v.findViewById(R.id.search_timevalue);
            choice = (TextView) v.findViewById(R.id.search_timechoice);
            timebutton = (RelativeLayout) v.findViewById(R.id.search_timebutton);
        }
    }

    class ButtonHoldler {
        TextView title;
        LinearLayout search_l_button;

        ButtonHoldler(View v) {
            title = (TextView) v.findViewById(R.id.search_itemtitle);
            search_l_button = (LinearLayout) v.findViewById(R.id.search_l_button);
        }
    }

    class ChoiceHoldler {
        RelativeLayout item_button;
        TextView name,value,right;

        ChoiceHoldler(View v) {
            item_button = (RelativeLayout) v.findViewById(R.id.jump_item);
            name = (TextView) v.findViewById(R.id.jump_title);
            value = (TextView) v.findViewById(R.id.jump_value);
            right = (TextView) v.findViewById(R.id.jump_right);
        }
    }

    class FinishHoldler {
        TextView reflash, finish;

        FinishHoldler(View v) {
            reflash = (TextView) v.findViewById(R.id.search_reflash);
            finish = (TextView) v.findViewById(R.id.search_finish);
        }
    }

    /**
     * 加载图标*
     */
    TextView check1 = null;
    TextView check2 = null;
    TextView check3 = null;

    private void addViewbutton(LinearLayout add, SearchBean bean) {
        add.removeAllViews();
        for (SystemTypeBean s : bean.getBeans()) {
            TextView view = new TextView(context);
            view.setWidth(0);
            view.setText(s.getTypename());
            view.setTextSize(14);
            view.setTextColor(context.getResources().getColor(R.color.login_words));
            view.setGravity(Gravity.CENTER);
            if (bean.getValueCode().equals(s.getTypecode())) {
                view.setBackgroundResource(R.drawable.circular6_blue_line3);
                view.setTextColor(context.getResources().getColor(R.color.wheel_timebtn_nor));
                if (bean.getTitle().equals(Constant.设计阶段)) {
                    setUncheck(bean, check1);
                    check1 = view;
                } else if (bean.getTitle().equals(Constant.项目类型)) {
                    setUncheck(bean, check2);
                    check2 = view;
                } else {
                    setUncheck(bean, check3);
                    check3 = view;
                }
            } else {
                view.setBackgroundResource(R.drawable.grayline3_c6_wihteitem);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
            int length = DensityUtil.dip2px(context, 6);
            layoutParams.setMargins(length, length, length, length);//4个参数按顺序分别是左上右下
            layoutParams.gravity = Gravity.CENTER;
            view.setOnClickListener(new MClickListener(0, bean, s) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    SearchBean searchBean = (SearchBean) msg;
                    switch (searchBean.getTitle()) {
                        case Constant.设计阶段:
                            setUncheck(searchBean, check1);
                            check1 = (TextView) view;
                            setCheck(searchBean, msg2, view);
                            break;
                        case Constant.项目类型:
                            setUncheck(searchBean, check2);
                            check2 = (TextView) view;
                            setCheck(searchBean, msg2, view);
                            break;
                        case Constant.项目进度:
                            setUncheck(searchBean, check3);
                            check3 = (TextView) view;
                            setCheck(searchBean, msg2, view);


                            break;
                    }

                }
            });
            add.addView(view, layoutParams);
        }
    }

    private void setCheck(SearchBean searchBean, Object msg2, View view) {
        searchBean.setValue(((SystemTypeBean) msg2).getTypename());
        searchBean.setValueCode(((SystemTypeBean) msg2).getTypecode());

        view.setBackgroundResource(R.drawable.circular6_blue_line3);
        ((TextView) view).setTextColor(context.getResources().getColor(R.color.wheel_timebtn_nor));
    }


    private void setUncheck(SearchBean searchBean, TextView view) {
        if (view != null) {
            searchBean.setValue("");
            searchBean.setValueCode("");
            view.setBackgroundResource(R.drawable.grayline3_c6_wihteitem);
            view.setTextColor(context.getResources().getColor(R.color.login_words));
        } else {
            searchBean.setValue("");
            searchBean.setValueCode("");
        }
    }
}
